package cn.itsource.org.controller;

import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.PageResult;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "部门的API", description = "部门相关的CRUD功能")
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    //注意前端地址：http://localhost:8080/department - get
    //查询所有
    @GetMapping
    @ApiOperation("查询所有")
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    //注意前端地址：http://localhost:8080/department/1 - get
    //根据id查询
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Department findOne(@PathVariable("id") Long id) {
        return departmentService.findOne(id);
    }

    //注意前端地址：http://localhost:8080/department/1 - delete
    //根据id删除
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
            departmentService.delete(id);
            return new AjaxResult();
    }


    //注意前端地址：http://localhost:8080/department - put - {}
    @PutMapping
    @ApiOperation("添加修改")
    public AjaxResult addorUpdate(@RequestBody Department department) {

        if (department.getId() == null) {//添加操作
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }
        //如果成功
        return new AjaxResult();
    }

    //注意前端地址：http://localhost:8080/department - Patch - []
    //批量删除
    @PatchMapping
    @ApiOperation("批量删除")
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        departmentService.patchdel(ids);
        return new AjaxResult();
    }

    //分页
    @PostMapping
    @ApiOperation("分页查询")
    public PageResult<Department> queryPage(@RequestBody DepartmentQuery query) {
        return departmentService.queryPage(query);
    }

    //接口：查询部门树
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }

}
